#!/usr/bin/env python3
# -*- coding:utf-8 -*-
## author : cypro666
## date   : 2014.5.29
"""
Global configs and constant defines
"""
import logging, os, sys

__all__ = ['MONGO_ADDR', 'MONGO_HOST', 'MONGO_PORT', 'MONGO_DEFAULTDB',
           'REQUEST_TIMEOUT', 'MAX_THREADS', 'CRAWL_SCHED', 'setlogging']

MONGO_ADDR = ('localhost', 27017)
MONGO_HOST, MONGO_PORT = MONGO_ADDR
MONGO_DEFAULTDB = 'xda'

REQUEST_TIMEOUT = 30  # min timeout of crawler's requests

MAX_THREADS = 512  # max threads of multi-thread-crawler

CRAWL_SCHED = (1024)  # schedule val of parallel spider


URL_SUFFIX_FILTER_2 = { 
    b'.rm', b'.7z', b'.ts', b'.gz'
}

URL_SUFFIX_FILTER_3 = { 
    b'.avi', b'.mp3', b'.mp4', b'.flv', b'.wmv', b'.dat',
    b'.asf', b'.swf', b'.mkv', b'.mpg', b'.vob', b'.3gp',
    b'.png', b'.bmp', b'.jpg', b'.gif', b'.pdf', b'.doc',
    b'.rar', b'.zip', b'.iso', b'.exe', b'.msi', b'.mpq',
    b'.cab', b'.dll', b'.pak', b'.apk', b'.ipa', b'.deb',
    b'.css' 
}

URL_SUFFIX_FILTER_4 = { 
    b'.rmvb', b'.mpeg', b'.jpeg', b'.gzip'
}


def setlogging(logfile):
    """Set and init log settings if need syslog dbsrv, can call only once"""
    mode = 'w'
    level = logging.NOTSET
    datefmt = '%y-%m-%d %H:%M:%S'
    msgformat = '%(asctime)s %(levelname)-8s %(module)s.%(funcName)s %(lineno)s: %(message)s'
    logging.basicConfig(filename=logfile,
                        filemode=mode,
                        level=level,
                        datefmt=datefmt,
                        format=msgformat)
    sys.stdout.write('logging file : %s\n' % os.path.realpath(logfile))


def test():
    sys.stdout.write('Mongodb address/port: %s\n' % str(MONGO_ADDR))
    sys.stdout.write('Mongodb default database: %s\n' % MONGO_DEFAULTDB)
    sys.stdout.write('Crawler\'s default timeout: %s\n' % REQUEST_TIMEOUT)
    sys.stdout.write('Crawler\'s max threads number: %s\n' % MAX_THREADS)
    sys.stdout.write('Schedule value of parallel spider: %s\n' % CRAWL_SCHED)
    testlogfile = 'test.log'
    setlogging(testlogfile)
    logging.info('test ok')


if __name__ == '__main__':
    test()

